﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
using System.Windows.Forms;

namespace PPE4_Diaz
{
    class model
    {
        
        private MySqlConnection myConnection;
        private bool connopen = false; 
        private MySqlDataAdapter mySqlDataAdapterPPE4 = new MySqlDataAdapter();
        private DataSet dataSetPPE4 = new DataSet();
        private DataView dvcateg_soins = new DataView(), dvtype_soins = new DataView(), dvsoins = new DataView();
        #region Get/Set
        public bool Connopen
        {
            get { return connopen; }
            set { connopen = value; }
        }
        private bool errgrave = false;

        public bool Errgrave
        {
            get { return errgrave; }
            set { errgrave = value; }
        }
        private bool chargement = false;

        public bool Chargement
        {
            get { return chargement; }
            set { chargement = value; }
        }
        #endregion
        #region Get/Set DataView
        public DataView Dvsoins
        {
            get { return dvsoins; }
            set { dvsoins = value; }
        }

        public DataView Dvtype_soins
        {
            get { return dvtype_soins; }
            set { dvtype_soins = value; }
        }

        public DataView Dvcateg_soins
        {
            get { return dvcateg_soins; }
            set { dvcateg_soins = value; }
        }
        #endregion

        public void seconnecter()
        {
            string myConnectionString = "Database=ppe4;Server=localhost;User Id=root;Password=";
            myConnection = new MySqlConnection(myConnectionString);
            try // tentative 
            {
                myConnection.Open();
                connopen = true;
            }
            catch (Exception err)// gestion des erreurs
            {
                MessageBox.Show("Erreur ouverture bdd : " + err, "PBS connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                connopen = false; errgrave = true;
            }
        }


        public void sedeconnecter()
        {
            if (!connopen)
                return;
            try
            {
                myConnection.Close();
                myConnection.Dispose();
                connopen = false;
            }
            catch (Exception err)
            {
                MessageBox.Show("Erreur fermeture bdd : " + err, "PBS deconnection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errgrave = true;
            }
        }


        public void import()
        {
            if (!connopen)
                return;
            mySqlDataAdapterPPE4.SelectCommand = new MySqlCommand("select * from categ_soins;select * from type_soins;select * from soins;", myConnection);
            try
            {
                dataSetPPE4.Clear();
                mySqlDataAdapterPPE4.Fill(dataSetPPE4);
                MySqlCommand vcommand = myConnection.CreateCommand();
                vcommand.CommandText = "SELECT AUTO_INCREMENT as last_id FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'type_soins'"; // attention si plusieurs table type_soins dans les bdd diverses plante donc personnalisez le nom de la table type_soins 
                UInt64 dertype_soins = (UInt64)vcommand.ExecuteScalar();
                vcommand.CommandText = "SELECT AUTO_INCREMENT as last_id FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'soins'"; // attention si plusieurs table soins idem 
                UInt64 dersoins = (UInt64)vcommand.ExecuteScalar();
                dataSetPPE4.Tables[1].Columns[0].AutoIncrement = true;
                dataSetPPE4.Tables[1].Columns[0].AutoIncrementSeed = Convert.ToInt64(dertype_soins);
                dataSetPPE4.Tables[1].Columns[0].AutoIncrementStep = 1;
                dataSetPPE4.Tables[2].Columns[0].AutoIncrement = true;
                dataSetPPE4.Tables[2].Columns[0].AutoIncrementSeed = Convert.ToInt64(dersoins);
                dataSetPPE4.Tables[2].Columns[0].AutoIncrementStep = 1;
                dvcateg_soins = dataSetPPE4.Tables[0].DefaultView;
                dvtype_soins = dataSetPPE4.Tables[1].DefaultView;
                dvsoins = dataSetPPE4.Tables[2].DefaultView;
                chargement = true;

            }
            catch (Exception err)
            {
                MessageBox.Show("Erreur chargement dataset : " + err, "PBS type_soins", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errgrave = true;
            }
        }






    }
}
